﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace template_dll
{
    public partial class Form2 : Form
    {
        public SqlConnection conn;
        private CurrencyManager cm;
        public Form2()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            HideShowRows();
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            HideShowRows();
        }

        private void Form2_Load(object sender, EventArgs e)
        {        
            dateTimePicker1.Value = DateTime.Today.AddDays(-1);
            LoadRevenue();
        }

        private void LoadRevenue()
        {
            SqlCommand getdate = conn.CreateCommand();
            getdate.CommandText = @"pas_RevenueCorrectSel";
            getdate.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            if (conn.State == ConnectionState.Closed) conn.Open();
            dt.Load(getdate.ExecuteReader());
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = dt.DefaultView;
            if (dataGridView1.DataSource != null)
            {
                cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource];
                dataGridView1.Sort(dataGridView1.Columns[1], ListSortDirection.Descending);
            }
        }

        private void HideShowRows()
        {
            cm.SuspendBinding();
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                r.Visible = true;
                r.DefaultCellStyle.BackColor = SystemColors.ControlLightLight;

                if (!Regex.Match(Convert.ToString(dataGridView1[dataGridView1.SortedColumn.Name, r.Index].Value), textBox1.Text, RegexOptions.IgnoreCase).Success)
                    r.Visible = false;
            }
            cm.ResumeBinding();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar)))// && !((e.KeyChar == '.') && (textBox2.Text.IndexOf(".") == -1) && (textBox1.Text.Length != 0)))
            {
                if (e.KeyChar != (char)Keys.Back)
                {
                    e.Handled = true;
                }
            }
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar)) && !((e.KeyChar == '.') && (textBox4.Text.IndexOf(".") == -1) && (textBox4.Text.IndexOf(",") == -1) && (textBox4.Text.Length != 0)) && !((e.KeyChar == ',') && (textBox4.Text.IndexOf(".") == -1) && (textBox4.Text.IndexOf(",") == -1) && (textBox4.Text.Length != 0)))
            {
                if (e.KeyChar != (char)Keys.Back)
                {
                    e.Handled = true;
                }
            }
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar)) && !((e.KeyChar == '.') && (textBox5.Text.IndexOf(",") == -1) && (textBox5.Text.IndexOf(".") == -1) && (textBox5.Text.Length != 0)) && !((e.KeyChar == ',') && (textBox5.Text.IndexOf(",") == -1) && (textBox5.Text.IndexOf(".") == -1) && (textBox5.Text.Length != 0)))
            {
                if (e.KeyChar != (char)Keys.Back)
                {
                    e.Handled = true;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlCommand getdate = conn.CreateCommand();
            getdate.CommandText = @"pas_RevenueCorrectInsDel";
            getdate.CommandType = CommandType.StoredProcedure;
            getdate.Parameters.AddWithValue("@pharm",Convert.ToInt32(textBox2.Text));
            getdate.Parameters.AddWithValue("@date",dateTimePicker1.Value.Date);
            getdate.Parameters.AddWithValue("@sum",Convert.ToDouble(textBox4.Text));
            getdate.Parameters.AddWithValue("@ch",Convert.ToInt32(textBox5.Text));
            getdate.Parameters.AddWithValue("@spr",checkBox1.Checked);
            getdate.Parameters.AddWithValue("@del",false);
            if (conn.State == ConnectionState.Closed) conn.Open();
            getdate.ExecuteNonQuery();
            LoadRevenue();
            MessageBox.Show("Данные добавлены");
            textBox2.Text = "";
            dateTimePicker1.Value = DateTime.Today.AddDays(-1);
            textBox4.Text = "0";
            textBox5.Text = "0";
            checkBox1.Checked = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int ind = dataGridView1.CurrentRow.Index;
            if (ind > -1)
            {
                string spr = "нет";
                if(Convert.ToBoolean(dataGridView1[4,ind].Value)) spr = "да";
                DialogResult dr =
                MessageBox.Show("Код аптеки " + dataGridView1[0,ind].Value.ToString() +
                    "; Дата " + dataGridView1[1, ind].Value.ToString() +
                    "; Сумма " + dataGridView1[2,ind].Value.ToString() +
                    "; Чеки " + dataGridView1[3,ind].Value.ToString() +
                    "; Справка-доставка " + spr,"Удалить строку?",MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    SqlCommand getdate = conn.CreateCommand();
                    getdate.CommandText = @"pas_RevenueCorrectInsDel";
                    getdate.CommandType = CommandType.StoredProcedure;
                    getdate.Parameters.AddWithValue("@pharm", dataGridView1[0, ind].Value);
                    getdate.Parameters.AddWithValue("@date", dataGridView1[1, ind].Value);
                    getdate.Parameters.AddWithValue("@sum", dataGridView1[2, ind].Value);
                    getdate.Parameters.AddWithValue("@ch", dataGridView1[3, ind].Value);
                    getdate.Parameters.AddWithValue("@spr", dataGridView1[4, ind].Value);
                    getdate.Parameters.AddWithValue("@del", true);
                    if (conn.State == ConnectionState.Closed) conn.Open();
                    getdate.ExecuteNonQuery();
                    LoadRevenue();
                }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int ind = dataGridView1.CurrentRow.Index;
            textBox2.Text = dataGridView1[0, ind].Value.ToString();
            dateTimePicker1.Value = Convert.ToDateTime(dataGridView1[1, ind].Value);
            textBox4.Text = dataGridView1[2, ind].Value.ToString();
            textBox5.Text = dataGridView1[3, ind].Value.ToString();
            checkBox1.Checked = Convert.ToBoolean(dataGridView1[4, ind].Value);
        }
    }
}
